local SceneBase = require("app.Base.SceneBase")

local CalculatorScene = class("CalculatorScene", SceneBase)

function CalculatorScene:ctor()
    CalculatorScene.super.ctor(self)
    print("CalculatorScene")
    self:initUI()

    self.vText = {}
    self.vTips_text = {}
    self.vNums = {}
end

function CalculatorScene:onEnter()
    CalculatorScene.super.onEnter(self)
    self:AddButtonClick()
end

function CalculatorScene:initUI()
    CalculatorScene.super.initUI(self)    
    self.title = td.CreateLabel("万象活动池赋魂脸黑计算器")
    td.AddRelaPos(self.bg,self.title,cc.p(0.5,0.85))

    --先拟定基础概率（单抽出金卡概率）和十连不出后的加成
    self.text_danchouchujinlv = td.CreateLabel("单抽出金率（%）")
    self.text_danchouchujinlv:addTo(self.bg):move(200,600):setAnchorPoint(1,0.5)

    self.danchouchujinlv = td.CreateEditBox({text = "1" }):move(250,600):addTo(self.bg)    

    self.text_shilianbuchujiacheng = td.CreateLabel("十连不出加成（%）")
    self.text_shilianbuchujiacheng:addTo(self.bg):move(500,600):setAnchorPoint(1,0.5)

    self.shilianbuchujiacheng = td.CreateEditBox({text = "20"}):move(550,600):addTo(self.bg)
    --然后选择抽卡方式（单抽/十连，十连不出会提高概率）
    self.danchou_btn = td.CreateBtn("单抽"):addTo(self.bg):move(800,600)

    self.shilian_btn = td.CreateBtn("十连"):addTo(self.bg):move(1000,600)
    --然后自动计算连抽100次/10次不出的概率
end

function CalculatorScene:AddButtonClick()
--    td.BtnAddTouch(self.danchou_btn)
    td.BtnAddTouch(self.shilian_btn,function() self:Shilianchou() end)
    td.BtnAddTouch(self.danchou_btn,function() self:Danchou() end)
end

function CalculatorScene:Danchou()
    for i, text in ipairs(self.vText) do
        text:removeFromParent()
    end
    self.vText={}
    self.vTips_text[1] = td.CreateLabel("没有单抽的，单抽出金卡欧皇快滚",td.RED,24*3)
    td.AddRelaPos(self.bg,self.vTips_text[1])
    self.vTips_text[2] = td.CreateLabel("其实是还没做好",cc.c3b(200,200,200),24)
    td.AddRelaPos(self.bg,self.vTips_text[2],cc.p(0.5,0.4))
end

function CalculatorScene:Shilianchou()
    for i, text in ipairs(self.vTips_text) do
        text:removeFromParent()
    end
    self.vTips_text={}
    print("十连计算")
    for i=1,10 do   --打印十行字
        local str = self:shilian_CreateText(i)
        local text = self.vText[i]
        if text == nil then 
            text = td.CreateLabel(str)
            text:move(10,550-i*50):addTo(self.bg):setAnchorPoint(0,0.5)
            table.insert(self.vText,text)
        else
            text:setString(str)
        end
    end    
end

function CalculatorScene:shilian_CreateText(i)
    local str_1 = "第"..i.."轮： "

    local nums = {}
    table.insert(nums,tonumber(self.danchouchujinlv:getText())*(100+(i-1)*tonumber(self.shilianbuchujiacheng:getText()))/10000)
    table.insert(nums,1-math.pow(1-nums[1],10)) 
    table.insert(nums,math.pow(1-nums[1],10))
    local lastNums = self.vNums[i-1] or {[4]=1}
    table.insert(nums,math.pow(1-nums[1],10)*lastNums[4]) 
    local lastSum = self.vNums[i-1] or {[5]=0}
    table.insert(nums,lastSum[5]+nums[2]) 
    self.vNums[i] = nums
    local str_2 = "【本轮】单出金："..(math.ceil(nums[1]*1000)/10).."% "..
    "十出金："..(math.ceil(nums[2]*1000)/10).."% "..
    "十不金："..(math.ceil(nums[3]*1000)/10).."% "..
    "【累计】不金:"..(math.ceil(nums[4]*1000)/10).."% "..
    "金期望："..math.ceil(nums[5]*100)/100


    local str = str_1..str_2
    return str
end

return CalculatorScene